﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebService_Components_ProjectGroup : GetTogether.Studio.Web.UI.Control
{
    public GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter> Projects;
    protected System.Text.StringBuilder Html = new System.Text.StringBuilder();
    protected System.Text.StringBuilder HtmlRecentUsed = new System.Text.StringBuilder();
    protected System.Text.StringBuilder HtmlRecentUsedProxy = new System.Text.StringBuilder();
    protected void Page_Load(object sender, EventArgs e)
    {
        Projects = GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter>.GetProjects<GetTogether.Studio.WebService.ProjectParameter>(GetTogether.Studio.ProjectParameter.ProjectCategory.WebService, CurrentSession.UserCode);
        Dictionary<string, GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter>> projectGrouped = new Dictionary<string, GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter>>();
        foreach (GetTogether.Studio.Project<GetTogether.Studio.WebService.ProjectParameter> p in Projects)
        {
            if (!projectGrouped.ContainsKey(p.Parameter.Category)) projectGrouped[p.Parameter.Category] = new GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter>();
            projectGrouped[p.Parameter.Category].Add(p);
        }
        int index = 1;
        foreach (string key in projectGrouped.Keys)
        {
            WebService_Components_Projects controlProject = (WebService_Components_Projects)Page.LoadControl("~/WebService/Components/Projects.ascx");
            controlProject.Projects = projectGrouped[key];
            bool isShowAll = GetTogether.Utility.NumberHelper.ToInt(Request["show"], 0) == 1;
            controlProject.IsShow = (index < 3 || isShowAll);
            //Response.Write(controlProject.Html);
            Html.AppendLine(controlProject.Html);
            index++;
        }
        if (Projects != null && Projects.Count > 0)
        {
            GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter> recentUsed = new GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter>();
            int count = 0;
            foreach (GetTogether.Studio.Project<GetTogether.Studio.WebService.ProjectParameter> pp in Projects)
            {
                recentUsed.Add(pp);
                count++;
                if (count>=15) break;
            }
            WebService_Components_ProjectsRecentUsed controlProject = (WebService_Components_ProjectsRecentUsed)Page.LoadControl("~/WebService/Components/ProjectsRecentUsed.ascx");
            controlProject.Projects = recentUsed;
            controlProject.IsShow = true;
            HtmlRecentUsed.AppendLine(controlProject.Html);
        }

        if (Projects != null && Projects.Count > 0)
        {
            Projects.SortBy("RecentUsedAction", false);
            GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter> recentUsedProxy = new GetTogether.Studio.Projects<GetTogether.Studio.WebService.ProjectParameter>();
            int count = 0;
            foreach (GetTogether.Studio.Project<GetTogether.Studio.WebService.ProjectParameter> pp in Projects)
            {
                if (pp.RecentUsedAction == DateTime.MinValue) continue;
                recentUsedProxy.Add(pp);
                count++;
                if (count >= 15) break;
            }
            WebService_Components_ProjectsRecentUsedProxy controlProject = (WebService_Components_ProjectsRecentUsedProxy)Page.LoadControl("~/WebService/Components/ProjectsRecentUsedProxy.ascx");
            controlProject.Projects = recentUsedProxy;
            controlProject.IsShow = true;
            HtmlRecentUsedProxy.AppendLine(controlProject.Html);
        }
    }
}